import { CreateKey, GetKey } from '@/modules/Login'
import http from './http'
import { createAsyncThunk } from '@reduxjs/toolkit'
import { UserProfile, UserSubcount } from '@/modules/User'
import { PlayList, Songs } from '@/modules/MusicList'
import { Music } from '@/modules/Music'
import { Recommend } from '@/modules/Recommend'
import { MvData, Mvs, mvPlayUrl } from '@/modules/Mv'
import { changeSongDetail } from '@/store/musicList'
import { Lrc } from '@/modules/Lyric'

/* 二维码登录 */
/* 二维码 key 生成接口 */
export const getKey: any = createAsyncThunk("login/getKey", async (_data, { dispatch }) => {
    const { data } = await http.get<{ data: GetKey }>('/login/qr/key', { timestamp: new Date().getTime() })

    if (data.code == 200) {
        dispatch(createKey(data.unikey))
    }
})
/* 二维码生成图片 */
export const createKey = createAsyncThunk("login/getKey", async (key: string, { dispatch }) => {
    const res = await http.post<{
        code: number, data: CreateKey
    }>('/login/qr/create', {
        key: key,
        timestamp: new Date().getTime(),
        qrimg: true
    })
    console.log(res)

    if (res.code == 200) {
        return { qrimg: res.data.qrimg, key: key }
    } else {
        return { qrimg: 'false', key: '' }
    }
})
/* 检查二维码状态 */
export const checkKey = createAsyncThunk("login/checkKey", async (key: string, { dispatch }) => {
    const res = await http.get<{
        code: number,
        message: string,
        cookie: string
    }>(`/login/qr/check`, {
        key: key,
        timestamp: new Date().getTime(),
    })
    // console.log(res)
    return res.code
})

/* 账号信息 */
/* 获取账号信息 */
export const getUserAccount = createAsyncThunk('user/getUserAccount', async (_data, { dispatch }) => {
    const data = await http.get<{
        profile: UserProfile
    }>('/user/account')

    return data.profile;
})
/* 获取用户信息，歌单，收藏，mv，dj数量 */
export const getUserSubcount = createAsyncThunk('user/getUserSubcount', async () => {
    return await http.get<
        UserSubcount
    >('/user/subcount')
})

/* 歌单 */
/* 获取用户歌单 */
export const getPlayList = createAsyncThunk('user/getPlayList', async (uid: number) => {
    const res = await http.get<{
        playlist: PlayList[]
    }>('/user/playlist', {
        uid: uid
    })

    console.log(res)

    return res.playlist
})
/* 获取歌单详情 */
export const getPlayListDetail = createAsyncThunk('user/getPlayListDetail', async (id: number) => {
    const data = await http.get<{
        playlist: PlayList
    }>('/playlist/detail', {
        id: id
    })
    // console.log(data)
    return data.playlist
})
/* 获取歌单所有歌曲 */
export const getAllPlayList = createAsyncThunk('user/getAllPlayList', async ({ id, len }: { id: number, len: number }, { dispatch }) => {
    const res = await http.get<{
        songs: Songs[]
    }>(`/playlist/track/all?id=${id}&limit=${len}&offset=0`)
    return res.songs
})

/* 歌曲 */
/* 获得歌曲详细信息 */
export const getMusicDetail = createAsyncThunk('music/getMusicDetail', async (ids: number, { dispatch }) => {
    const { songs } = await http.get<{
        songs: Songs[]
    }>('/song/detail', {
        ids: ids
    })
    dispatch(changeSongDetail(songs[0]))
})
/* 获得歌曲的播放地址 */
export const getMusicUrl = createAsyncThunk('music/getMusicUrl', async (id: number, { dispatch }) => {
    const { data } = await http.get<{
        data: Music[]
    }>('/song/url', {
        id: id
    })
    dispatch(getMusicDetail(id))
    return data
})

/* 推荐 */
/* 每日推荐歌单 */
export const getResource = createAsyncThunk('find/getResource', async () => {
    const data = await http.get<{
        recommend: Recommend[]
    }>('/recommend/resource')

    // console.log(recommend)
    return data.recommend
})
/* 每日推荐歌曲 */
export const getRecommendSongs = createAsyncThunk('find/getRecommendSongs', async () => {
    const data = await http.get<{
        recommend: Recommend[]
    }>('/recommend/songs')
    console.log(data)
})

/* mv */
/* 获得mv详细信息 */
export const getMvUrl = createAsyncThunk('mv/getMuUrl', async (mvid: number) => {
    const { data } = await http.get<{
        data: MvData
    }>('/mv/detail', {
        mvid: mvid
    })
    // console.log(data)
    return data
})
/* 获得mv的地址 */
export const getMvPlay = createAsyncThunk('mv/getMvPlay', async (id: number) => {
    const { data } = await http.get<{
        data: mvPlayUrl
    }>('/mv/url', {
        id: id
    })
    return data
})
/* 相似mv */
export const getSimiMv = createAsyncThunk('mv/getSimiMv', async (mvid: number) => {
    const { mvs } = await http.get<{
        mvs: Mvs[]
    }>('/simi/mv', {
        mvid: mvid
    })
    return mvs
})

/* 歌词 */
export const getLyricWord = createAsyncThunk('music/getLyricWord', async (id: number) => {
    const { lrc } = await http.get<{
        lrc: Lrc
    }>('/lyric/new', {
        id: id
    })
    return lrc.lyric
})